<?php

namespace App\Models\DylistModels;

use App\Models\ParentModel;
use ClassesWithParents\D;
use DB;
use App\Components\Tools;
use Log;

/**
 * 抖音美食榜model
 * @author puppet
 */
class DyFood extends ParentModel
{
    /**
     * 与模型关联的数据表。
     *
     * @var string
     */
    protected $table = 'pt_dy_food_overall';

    /**
     * DB::直接操作的数据库表名
     * @var string
     */
    protected $db_table = 'pt_dy_food_overall';

    /**
     * 主键 默认id。
     *
     * @var string
     */
    protected $primaryKey = 'id';

    /**
     * 指定是否模型应该被戳记时间。
     *
     * @var bool
     */
    public $timestamps = false;


    //获取当天rank1的热度指数值
    function getMaxValue()
    {
        $sql = " select hot_level from $this->table  as a  where hot_level=(select max (b.hot_level) from $this->table as b where a.id=b.id) and to_days(create_time) = to_days(now())";
        $maxValue = DB::select($sql);
//        if (empty($maxValue)) {//如果当日数据为空 就显示昨天的数据
//            $sqll = " select hot_level from $this->table where type=1 and rank=1 and TO_DAYS( NOW( ) ) - TO_DAYS( create_time) = 1";
//
//            $maxValue = DB::select($sqll);
//        }
        print_r($maxValue);exit();
        return $maxValue[0]['hot_level'];
    }

    //插入美食榜数据
    function upFoodto($date)
    {
        set_time_limit(0);
        foreach ($date as $k => $v) {
            $data = array(
                'rank' => $v['rank'],
                'rank_change' => $v['rank_change'],
                'nickname' => $v['nickname'],
                'avatar' => $v['avatar'],
                'follower_count' => $v['follower_count'],
                'onbillbaord_times' => $v['onbillbaord_times'],
                'create_time' => date('Y-m-d H:i:s', time()),
                'update_time' => date('Y-m-d H:i:s', time()),
                'type' => 1,
                'effect_value' => $v['effect_value'],
            );
            $bid=DB::table($this->table)->insertGetId($data);
            $vurl = $v['video_list'];
            if (is_array($vurl)) {
                foreach ($vurl as $vk => $vv) {
                    $vdata = array(
                        'item_cover' => $vv['item_cover'],
                        'share_url' => $vv['share_url'],
                        'title' => $vv['title'],
                        'bid' => $bid,
                        'create_time' => date('Y-m-d H:i:s', time()),
                        'update_time' => date('Y-m-d H:i:s', time()),
                        'type' => 4,
                    );
                    //  echo '<pre/>';print_r($vdata);
                    DB::table("pt_dy_bangdan_video")->insertGetId($vdata);//把榜单视频数据按昵称存入库
                }

            }

        }
    }
}
